const Router = require('koa-router')
const { Article } = require('@/app/models/article')
const { Auth } = require('@/middleware/auth')
const {Sequelize, Op} = require('sequelize')
const { AddArticleValidator, setArticleChannelValidator, PositiveIntegerValidator, NotEmptyValidator } = require('@/utils/validator')

const router = new Router({
  //统一这是前缀
  prefix: '/api/v1/article' 
})

//新增文章

//status： 1：上线 2：下线 3：草稿 
router.post('/addArticle', async ctx => {
  const v = await new AddArticleValidator().validate(ctx)
  const articleInfo = {
    title: v.get('body.title'),
    channel_id: v.get('body.channelId'),
    content: v.get('body.content'),
    thumbnail_url: v.get('body.thumbnailUrl'),
    profile: v.get('body.profile'),
    identifying_id: v.get('body.identifyingId'),
    status: v.get('body.status') || 1
  }
  await Article.create(articleInfo)
  ctx.body = global.responseData(null, '增加文章成功')
})

//查询文章
router.post('/queryArticle', async ctx => {
  let { pageNumber,pageSize,channelId,status,articleName } = ctx.request.body
  const articles = await Article.scope('cd').findAndCountAll({
    order: [
      ['id', 'ASC']
    ],
    limit: pageNumber || 10,
    offset: ((pageSize || 1) - 1) * (pageNumber || 10),
    where: {
      channel_id:{
        [Op.or]: channelId ? [channelId] : [],
      },
      status:{
        [Op.or]: status ? [status] : [],
      },
      title: {
        // 模糊查询
        [Op.like]:'%' + articleName + '%'
      }
    },
  })
  ctx.body = global.responseData(articles)
})

async function setLineArticle(ctx, type){
  const v = await new NotEmptyValidator().validate(ctx,{
    token: 'ids'
  })
  const idArray = v.get('body.ids').split(',')
  const article = await Article.findAll({
    where:{
      id: idArray
    }
  })
  
  if(article.length > 0){
    article.forEach(item => {
      item.status = type == 'onlineArticle' ? 1 : 2
      item.save()
    });
    const text = type == 'onlineArticle' ? '上线文章成功' : '下线文章成功'
    ctx.body = global.responseData(null, text)
    
  }else{
    ctx.body = global.responseData(null, '未找到文章', '404')
    
  }
}
//上线文章
router.post('/onlineArticle',async ctx => {
  await setLineArticle(ctx, 'onlineArticle')
})
//下线文章
router.post('/outlineArticle',async ctx => {
  await setLineArticle(ctx, 'outlineArticle')
})
//删除文章
router.post('/deleteArticle',async ctx => {
  const v = await new PositiveIntegerValidator().validate(ctx)
  const article = await Article.findOne({
    where:{
      id: v.get('body.id')
    }
  })
  if(article){
    await article.destroy()
    ctx.body = global.responseData(null, '删除文章成功')
  }else{
    ctx.body = global.responseData(null, '未找到文章', '404')
  }
})
//选择分类
router.post('/setArticleChannel',async ctx => {
  const v = await new setArticleChannelValidator().validate(ctx)
  const idArray = v.get('body.ids').split(',')
  const article = await Article.findAll({
    where:{
      id: idArray
    }
  })
  
  if(article.length > 0){
    article.forEach(item => {
      item.channel_id = v.get('body.channelId')
      item.save()
    });
    ctx.body = global.responseData(null, '设置分类成功')
  }else{
    ctx.body = global.responseData(null, '未找到文章', '404')
    
  }
})

module.exports = router